#include <iostream>
#include <cstdlib>

using namespace std;

void swap(int &num_1, int &num_2)
{
    int temp = num_1;
    num_1 = num_2;
    num_2 = temp;
}

int main()
{
    //Wrong declaration of array.
    //int size = 10;
    //int myArray[size];

    //Correct declaration of array.
    const int size = 10;
    int myArray[size];

    int myArray_1[10], myArray_2[5], myArray_3[6], myArray_4[7];

    int myArray_5[3] = {1, 2, 3};
    int myArray_6[] = {1, 2, 3};
    srand(time(0));

    //Assign random values to array.
    for(int i = 0; i < 10; i++)
    {
        myArray[i] = rand() % 100;
    }

    //print the elements in the array.
    for(int i = 0; i < size; i++)
    {
        cout << "myArray[" << i << "] = " << myArray[i] << endl;
    }

    //copy arrays
    int array_copy[size];
    for(int i = 0; i < size; i++)
    {
        array_copy[i] = myArray[i];
        cout << "array_copy[" << i << "] = " << array_copy[i] << endl;
    }

    //copy arrays using reference variable
    int (&array_ref)[size] = myArray;
    for(int i = 0; i < size; i++)
    {
        cout << "array_ref[" << i << "] = " << array_ref[i] << endl;
    }

    //find max number in the array
//    int max = 0;//don't assign max to zero, because if all the numbers in myArray is negative, this will be an error.
    int max = myArray[0];
    for(int i = 0; i < size; i++)
    {
        if(myArray[i] > max)
        {
            max = myArray[i];
        }
    }

    cout << "The max number of myArray is: " << max << endl;

    //find the min index of the max element
    cout << "\n===========\nFind the min index of the max element." << endl;
    int myArray_10[30];
    int index_of_max = 0;
    for(int i = 0; i < 30; i++)
    {
        myArray_10[i] = rand() % 10;
        if(i == 0)
        {
            max = myArray_10[i];
        }
        cout << "myArray_10[" << i << "] = " << myArray_10[i] << endl;
        if(myArray_10[i] > max)
        {
            max = myArray_10[i];
            index_of_max = i;
        }
    }
   
    cout << "The max element is: " << max << ", its index is " << index_of_max << endl;

    //shuffle elements
    cout << "\n==========\nShuffle elements." << endl;
    cout << "Before shuffle." << endl;
    for(int i = 0; i < size; i++)
    {
        cout << "myArray[" << i << "] = " << myArray[i] << endl;
    }
    
    srand(time(0));
    cout << "After shuffle." << endl;
    for(int i = 0; i < size; i++)
    {
        int index_of_swap = rand() % size;
        swap(myArray[i], myArray[index_of_swap]);
    }
    
    for(int i = 0; i < size; i++)
    {
        cout << "myArray[" << i << "] = " << myArray[i] << endl;
    }

    //shift elements
    cout << "\n==========\nShift elements." << endl;
    cout << "Enter the direction you want to shift.\n1: left,\n2: right." << endl;
    int direction = 1;
    cin >> direction;
    cout << "Enter the step(s) you want to shift: " << endl;
    int steps = 0;
    cin >> steps;

    int myArray_11[size];
    for(int i = 0; i < size; i++)
    {   
        int index_new;
        if(direction == 1)
        {
            cout << "Shifting elements " << steps << " step(s) to left." << endl;
            index_new = ((i - steps) >= 0) ? (i - steps) : (i - steps + size);
            myArray_11[index_new] = myArray[i];
        }
        else
        {
            cout << "Shifting elements " << steps << " step(s) to right." << endl;
            index_new = ((i + steps) <= (size - 1) ? (i + steps) : (i + steps - size));
            myArray_11[index_new] = myArray[i];
        }
    }

    for(int i = 0; i < size; i++)
    {
        cout << "myArray_11[" << i << "] = " << myArray_11[i] << endl;
    }

    return 0;
}
